﻿var LandscapeFileUploader = {

    ClearFilesToUpload: function () {
        var uploader = this.GetUploader();
        uploader.deleteAllFileInputs();
    },

    Reset: function () {
        var uploader = this.GetUploader();
        uploader.deleteAllFileInputs();
        var grid = this.GetFileGrid();
        grid.get_masterTableView().set_dataSource(new Array());
        grid.get_masterTableView().dataBind();
    },

    LoadFiles: function (pEntityID) {
        var grid = this.GetFileGrid();
        var pageSize = grid.get_masterTableView().get_pageSize();
        var pageNo = grid.get_masterTableView().get_currentPageIndex();

        if (this._ClientGetData != null && this._ClientGetData.trim() != '') {
            eval(this._ClientGetData)(pEntityID, pageNo, pageSize);
        }
        else {
            N_NotifyMessage('Error', 'OnGetData is not implimented. Please add client script to fetch the data.', 2);
        }
    },

    BindFileList: function (pFileList) {
        var grid = this.GetFileGrid();
        grid.get_masterTableView().set_dataSource(pFileList);
        grid.get_masterTableView().dataBind();
    },

    BindFileCount: function (pCount) {
        var grid = this.GetFileGrid();
        grid.get_masterTableView().set_virtualItemCount(pCount);
    },

    GetUploadedFiles: function () {
        var files = new Array();
        if (this.GetUploader().getUploadedFiles().length > 0) {
            for (var idx = 0; idx < this.GetUploader().getUploadedFiles().length; idx++) {
                var fileInfoObject = this.GetUploader().getUploadedFiles()[idx];
                var fileItem = new Landscape.Attachment();
                fileItem.Title = fileInfoObject;
                fileItem.Description = '';
                fileItem.FileName = fileInfoObject;
                fileItem.FileSize = 0;
                fileItem.UploadedBy = MASTER_GetCurrentUserCode();

                files.push(fileItem);
            }
        }

        return files;
    },

    DefaultRowDataBind: function (sender, args) {
        var cell = args.get_item().get_cell('ActionsColumn');
        var anchor = String.format("<a href='#' onclick='return {2}(\"{0}\");'><img alt=\"Download\" src=\"{1}\" style=\"border:0px\" /></a>", args.get_dataItem().RepositoryCode, ResolveUrl("~/Images/save_download_32.png"), this._ClientFileDownloadClick);

        if (this._ClientFileDeleteClick != null && this._ClientFileDeleteClick.trim() != '')
            anchor += String.format("&nbsp&nbsp<a href='#' onclick='return {2}(\"{0}\");'><img alt=\"Delete\" src=\"{1}\" style=\"border:0px\" /></a>", args.get_dataItem().RepositoryCode, ResolveUrl("~/Images/Delete_32x32.png"), this._ClientFileDeleteClick);

        cell.innerHTML = anchor;
        var cell1 = args.get_item().get_cell('FileSizeColumn');
        var fs = args.get_dataItem().FileSize;
        var fsType = 'MB';
        if (((args.get_dataItem().FileSize / 1024) / 1024) >= 1) {
            fs = ((args.get_dataItem().FileSize / 1024) / 1024);
        }
        else {
            if ((args.get_dataItem().FileSize / 1024) >= 1) {
                fs = (args.get_dataItem().FileSize / 1024);
                fsType = 'KB';
            }
            else {
                fs = args.get_dataItem().FileSize;
                fsType = 'B';
            }
        }

        cell1.innerHTML = fs.toFixed(2) + ' ' + fsType;

        if (this._ClientGridRowDataBound != null && this._ClientGridRowDataBound.trim() != '') {
            eval(this._ClientGridRowDataBound)(sender, args);
        }
    },

    DefaultGridCreated: function (sender, args) {
        sender.get_masterTableView().set_dataSource('');
        sender.get_masterTableView().dataBind();

        if (this._ClientGridCreated != null && this._ClientGridCreated.trim() != '') {
            eval(this._ClientGridCreated)(sender, args);
        }
    },

    DefaultGridCommand: function (sender, args) {
        if (this._ClientGridCommand != null && this._ClientGridCommand.trim() != '') {
            eval(this._ClientGridCommand)(sender, args);
        }

        return;
    }
};